package com.sservice.service.service.impl;

import com.sservice.model.dao.OtherReceivablesMapper;
import com.sservice.model.entity.OtherReceivables;
import com.sservice.model.vo.OtherReceivablesVo;
import com.sservice.service.service.IAdvanceReceiptService;
import com.sservice.service.service.IOrderAdvanceReceiptService;
import com.sservice.service.service.OtherReceivablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by suweiming on 2017/12/20.
 */

@Service
public class OtherReceivablesServiceImpl implements OtherReceivablesService {
    @Autowired
    private OtherReceivablesMapper dao;
    @Autowired
    private IAdvanceReceiptService advanceReceiptService;
    @Autowired
    private IOrderAdvanceReceiptService orderAdvanceReceiptService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OtherReceivables record) {
        return dao.insert(record);
    }

    @Override
    public int insertSelective(OtherReceivables record) {
        return dao.insertSelective(record);
    }

    @Override
    public OtherReceivables selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(OtherReceivables record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Transactional
    @Override
    public Object payByCash(OtherReceivables record) {
        BigDecimal receivables = record.getReceivables();
        String creator = record.getCreator();
        record.setCreator(null);
        record.setReceivables(null);
        int it = dao.updateByPrimaryKeySelective(record);
        if (it > 0) {
//            AdvanceReceipt ar = new AdvanceReceipt();
//            ar.setAccount(String.valueOf(record.getPayAccount()));
//            ar.setType(record.getType()); // 付款类型 0 收款, 1 付款
//            ar.setTotalDeposit(receivables);
//            ar.setTotalVerified(receivables);
//            ar.setVerifiedOver("1"); // 核销完毕
//            ar.setOperator(creator);
//            ar.setPayType(String.valueOf(record.getPayMethod()));
//            ar.setClienteleId(record.getClienteleId());
//            ar.setUnitCode(record.getUnitCode());
//            ar.setPayTime(record.getActualPayTime());
//            ar.setRemark("其他'" + record.getName() + "'应" + payTypeStr(record.getType()) + "现金付款");
//            advanceReceiptService.insertSelective(ar);
//            OrderAdvanceReceipt oar = new OrderAdvanceReceipt();
//            oar.setType("0");
//            oar.setOperator(ar.getOperator());
//            oar.setRemark("其他应" + payTypeStr(record.getType()) + "现金付款");
//            oar.setOrderId(String.valueOf(record.getId()));
//            oar.setPayCash(ar.getTotalDeposit());
//            oar.setAdvanceReceiptId(String.valueOf(ar.getId()));
//            orderAdvanceReceiptService.insertSelective(oar);
        } else {
            return "付款失败!";
        }

        return it;
    }

    @Override
    public String payTypeStr(String type) {
        if (type.equals("0")) {
            return "付";
        } else if (type.equals("1")) {
            return "收";
        }
        return "";
    }

    @Override
    public int updateByPrimaryKey(OtherReceivables record) {
        return dao.updateByPrimaryKey(record);
    }

    @Override
    public int queryByCount(OtherReceivablesVo record) {
        return dao.queryByCount(record);
    }

    @Override
    public List<OtherReceivablesVo> queryByPage(OtherReceivablesVo record) {
        return dao.queryByPage(record);
    }
}
